package com.dogsong.liteflow.editor.component;

import com.dogsong.liteflow.editor.constant.ContextConstant;
import com.dogsong.liteflow.editor.slot.DataContext;
import com.yomahub.liteflow.core.NodeBooleanComponent;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component("numberBooleanCmp")
public class NumberBooleanCmp extends NodeBooleanComponent{
    @Override
    public boolean processBoolean() throws Exception {
        // el表达式通过bind表达式绑定两个输入和对应的表达式
        DataContext dataContext = this.getContextBean(DataContext.class);
        Number i1 = (Number)dataContext.getDataMap().get(this.getBindData(ContextConstant.INPUT_1_DATA, String.class));
        Number i2 = (Number)dataContext.getDataMap().get(this.getBindData(ContextConstant.INPUT_2_DATA, String.class));
        String compare = ((Map<String,String>) this.getRequestData()).get(this.getTag());

        if (i1 == null || i2 == null) {
            throw new IllegalArgumentException("比较值不能为空");
        }

        double d1 = i1.doubleValue();
        double d2 = i2.doubleValue();

        switch (compare) {
            case ">":
                return d1 > d2;
            case "<":
                return d1 < d2;
            case ">=":
                return d1 >= d2;
            case "<=":
                return d1 <= d2;
            case "==":
            case "=":
                return d1 == d2;
            case "!=":
                return d1 != d2;
            default:
                throw new IllegalArgumentException("不支持的数字比较操作: " + compare);
        }
    }
}
